package dao

import (
	"TTMS/model"
	"TTMS/utils"
	"database/sql"
)

// GetFilms 获取所有的电影
func GetFilms() ([]*model.Film, error) {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film"
	//执行
	rows, err := utils.DB.Query(sqlStr)
	if err != nil {
		return nil, err
	}
	var films []*model.Film
	for rows.Next() {
		film := &model.Film{}
		rows.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
		films = append(films, film)
	}
	return films, nil
}

// ClassifyFilmByState 通过 State 对电影进行分类
func ClassifyFilmByState() ([]*model.Film, []*model.Film, error) {
	films, err := GetFilms()
	if err != nil {
		return nil, nil, err
	}
	var showingFilms []*model.Film
	var upcomingFilms []*model.Film
	for _, v := range films {
		if v.State == 0 {
			showingFilms = append(showingFilms, v)
		} else if v.State == 1 {
			upcomingFilms = append(upcomingFilms, v)
		}
	}
	return showingFilms, upcomingFilms, nil
}

// ClassifyFilms 通过 State、FilmType、Zone 对电影进行筛选
func ClassifyFilms(state int, filmType, zone string) ([]*model.Film, error) {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film where state=?"
	//执行
	rows := &sql.Rows{}
	var err error
	if filmType == "全部" && zone == "全部" {
		rows, err = utils.DB.Query(sqlStr, state)
	} else if filmType == "全部" {
		sqlStr += " and zone =?"
		rows, err = utils.DB.Query(sqlStr, state, zone)
	} else if zone == "全部" {
		sqlStr += " and film_type=?"
		rows, err = utils.DB.Query(sqlStr, state, filmType)
	} else {
		sqlStr += " and film_type=? and zone =?"
		rows, err = utils.DB.Query(sqlStr, state, filmType, zone)
	}
	if err != nil {
		return nil, err
	}
	var films []*model.Film
	for rows.Next() {
		film := &model.Film{}
		rows.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
		films = append(films, film)
	}
	return films, nil
}

// CheckFilmByName 根据电影名称查询电影
func CheckFilmByName(filmName string) *model.Film {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film where film_name=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, filmName)
	film := &model.Film{}
	row.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
	return film
}

// AddFilm 向数据库中添加电影
func AddFilm(film *model.Film) error {
	//写 sql 语句
	sqlStr := "insert into film (film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date) values (?,?,?,?,?,?,?,?,?,?,?);"
	//执行
	_, err := utils.DB.Exec(sqlStr, film.FilmName, film.FilmType, film.Duration, film.Price, film.Sales, film.TicketOffice, film.ImgPath, film.Zone, film.State, film.ScreenDate, film.OfflineDate)
	if err != nil {
		return err
	}
	return nil
}

// UpdateFilm 修改电影信息
func UpdateFilm(film *model.Film) error {
	//写 sql 语句
	sqlStr := "update film set film_type=?, duration=?, price=?, sales=?, ticket_office=?, zone=?, state=?, screen_date=?, offline_date=? where film_name=?;"
	//执行
	_, err := utils.DB.Exec(sqlStr, film.FilmType, film.Duration, film.Price, film.Sales, film.TicketOffice, film.Zone, film.State, film.ScreenDate, film.OfflineDate, film.FilmName)
	if err != nil {
		return err
	}
	return nil
}

// DeleteFilm 删除电影
func DeleteFilm(filmName string) error {
	//写 sql 语句
	sqlStr := "delete from film where film_name=?"
	//执行
	_, err := utils.DB.Exec(sqlStr, filmName)
	if err != nil {
		return err
	}
	return nil
}

// GetFilmsByDate 查询在 Date 时间热映的电影
func GetFilmsByDate(date string) ([]*model.Film, error) {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film where film.screen_date <= ? and film.offline_date >= ?"
	//执行
	rows, err := utils.DB.Query(sqlStr, date, date)
	if err != nil {
		return nil, err
	}
	var films []*model.Film
	for rows.Next() {
		film := &model.Film{}
		rows.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
		films = append(films, film)
	}
	return films, nil
}

// GetFilmsByName 根据电影名称查询电影
func GetFilmsByName(filmName string) *model.Film {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film where film_name=?"
	//执行
	row := utils.DB.QueryRow(sqlStr, filmName)
	film := &model.Film{}
	row.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
	return film
}

// CheckFilmByDateAndName 根据电影名称和时间判断该电影在改时间是否热映
func CheckFilmByDateAndName(date string, filmName string) *model.Film {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film where film_name=? and (film.screen_date <= ? and film.offline_date >= ?)"
	//执行
	row := utils.DB.QueryRow(sqlStr, filmName, date, date)
	film := &model.Film{}
	row.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
	return film
}

// FuzzySearch 模糊查找
func FuzzySearch(filmName string) ([]*model.Film, error) {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film where film_name like ?"
	//执行
	rows, err := utils.DB.Query(sqlStr, "%"+filmName+"%")
	if err != nil {
		return nil, err
	}
	var films []*model.Film
	for rows.Next() {
		film := &model.Film{}
		rows.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
		films = append(films, film)
	}
	return films, nil
}

// SortFilmByTicketOffice 根据票房给电影排序
func SortFilmByTicketOffice() ([]*model.Film, error) {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film order by ticket_office desc"
	//执行
	rows, err := utils.DB.Query(sqlStr)
	if err != nil {
		return nil, err
	}
	var films []*model.Film
	for rows.Next() {
		film := &model.Film{}
		rows.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
		films = append(films, film)
	}
	return films, nil
}

// SortFilmBySales 根据销量给电影排序
func SortFilmBySales() ([]*model.Film, error) {
	//写 sql 语句
	sqlStr := "select id,film_name, film_type, duration, price, sales, ticket_office, img_path, zone, state, screen_date, offline_date from film order by ticket_office desc"
	//执行
	rows, err := utils.DB.Query(sqlStr)
	if err != nil {
		return nil, err
	}
	var films []*model.Film
	for rows.Next() {
		film := &model.Film{}
		rows.Scan(&film.ID, &film.FilmName, &film.FilmType, &film.Duration, &film.Price, &film.Sales, &film.TicketOffice, &film.ImgPath, &film.Zone, &film.State, &film.ScreenDate, &film.OfflineDate)
		films = append(films, film)
	}
	return films, nil
}
